﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VAdvantage.Controls;

namespace VAdvantage.Grids
{
    public class VDataGridButtonColumn : DataGridBoundColumn, IDataGridColumn
    {

        private GridField _field;
        private Controls.VButton editingControl = null;
        private string _columnName = "";
        private object currentCellValue = null;
        private TextBlock textBlock =   null;

        public event RoutedEventHandler Button_Click = null;

        public string DataPropertyName
        {
            get
            {
                return _columnName;
            }
            set
            {
                _columnName = value.ToUpper();
                this.Binding = new System.Windows.Data.Binding(_columnName);
            }
        }

        public Controls.VDataGrid DataGridView
        {
            get;set;
        }

        public GridField Field
        {
            get
            {
                return _field;
            }
            set
            {
                _field = value;
                editingControl = new Controls.VButton();
                editingControl.SetAttribute(_field, _field.GetColumnName(), _field.IsMandatory(false), _field.IsReadOnly(), _field.IsEditable(false), _field.GetHeader(), _field.GetDescription(), _field.GetHelp(), _field.GetAD_Process_ID());
            }
        }

        public void Dispose()
        {
            if (editingControl != null)
            {
                editingControl.Dispose();
            }
            editingControl = null;
        }


        protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
        {
            if (cell.Content is VButton)
            {
                editingControl.Click -= new RoutedEventHandler(editingControl_Click);

                currentCellValue = editingControl.GetValue();
               // _field.SetValue(currentCellValue);
                //((DataUtil.DataObject)dataItem).SetFieldValue(_columnName, currentCellValue);
                //editingControl.SetValue(null);
                //editingControl                //currentBlock.Text = value == null ? "0" : value.ToString();
                //textBlock.Text = currentCellValue == null ? "" : currentCellValue.ToString();
                return textBlock;
            }

            TextBlock txt = Utility.AEnv.GetDataGridElement();

            if (this.Binding != null)
            {
                txt.SetBinding(TextBlock.TextProperty, this.Binding);
            }
            return txt;
        }

        protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
        {
            if (editingControl == null)
            {
                editingControl = new VButton();
                editingControl.SetAttribute(_field,_field.GetColumnName(),_field.IsMandatory(false),_field.IsReadOnly(),_field.IsEditable(false),_field.GetHeader(),_field.GetDescription(),_field.GetHelp(),_field.GetAD_Process_ID());
                editingControl.SetField(_field);
            }

            currentCellValue = ((DataUtil.DataObject)dataItem).GetFieldValue(_columnName);

            textBlock = (TextBlock)cell.Content;

            editingControl.SetValue(null);
            return editingControl;
        }

        protected override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs)
        {
           // if (editingEventArgs.OriginalSource is TextBlock)
            //{
               // textBlock = (TextBlock)editingEventArgs.OriginalSource;
               //editingControl.EditingControlDataGridView = DataGridView;
                editingControl.SetValue(currentCellValue);
                editingControl.Click += new RoutedEventHandler(editingControl_Click);
            //}
            return editingElement;
        }

        void editingControl_Click(object sender, RoutedEventArgs e)
        {
            if (Button_Click != null)
            {
                Button_Click(sender, e);
            }

        }


        public bool ReadOnly
        {

            get;
            set;

        }



    }
}
